package com.example.blog.web;

import com.example.blog.annotation.LoginUser;
import com.example.blog.annotation.RequiresPermission;
import com.example.blog.dao.PostClassInfoRepository;
import com.example.blog.dao.PostsInfoRepository;
import com.example.blog.dto.PostClassDto;
import com.example.blog.entity.PostClassInfo;
import com.example.blog.entity.PostsInfo;
import com.example.blog.exception.NewWifiLoginException;
import com.example.blog.util.JsonXMLUtils;
import com.example.blog.util.Result;
import com.example.blog.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Optional;

import static com.example.blog.constant.ApiUrlConstants.ADMIN;
import static com.example.blog.constant.ApiUrlConstants.USER;

/**
 * @ClassName: comments
 * @Description: 帖子评论

 * @Date 2023/9/9
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/v1")
@Api(value = "帖子分类管理",description = "帖子分类管理")
public class PostClassController {
    @Resource
    PostClassInfoRepository postClassInfoRepository;

    /**
     * 新增帖子
     * @param userId
     * @param postClassDto
     * @return
     */
    @PostMapping("/class")
    @ApiOperation(value = "帖子-分类-新增")
    @RequiresPermission(roles = {ADMIN})
    public Result addPost(@LoginUser String userId,@RequestBody PostClassDto postClassDto){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        postClassDto.setCreUser(userId);
        PostClassInfo postClassInfo = new PostClassInfo();
        BeanUtils.copyProperties(postClassDto,postClassInfo);
        PostClassInfo save = postClassInfoRepository.save(postClassInfo);
        return Result.ok().setData(save);
    }

    /**
     * 查看帖子
     * @param userId
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation(value = "帖子-分类-列表")
    public Result getPost(@LoginUser String userId, @RequestParam Integer pageSize, @RequestParam Integer pageNum){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        pageNum = pageNum == null || pageNum < 0 ? 0:pageNum-1;
        Pageable pageable = PageRequest.of(pageNum,pageSize); //页码：前端从1开始，jpa从0开始，做个转换
        Page<Map<String, Object>> byUset = postClassInfoRepository.findBy(pageable);
        return Result.ok().setData(byUset);
    }
    @ApiOperation(value = "帖子-分类-删除")
    @DeleteMapping("/class/{id}")
    public Result delPost(@LoginUser String userId,@PathVariable String id){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        postClassInfoRepository.deleteById(id);
        return Result.ok();
    }
    @ApiOperation(value = "分类-详情")
    @PostMapping("/class/{id}")
    @RequiresPermission(roles = {ADMIN})
    public Result getPost(@LoginUser String userId,@PathVariable String id){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        Optional<PostClassInfo> byId = postClassInfoRepository.findById(id);
        return Result.ok().setData(byId.get());
    }
    @ApiOperation(value = "分类-更新")
    @PutMapping("/class")
    @RequiresPermission(roles = {ADMIN})
    public Result uptPost(@LoginUser String userId,@RequestBody PostClassDto postsInfo){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        PostClassInfo postClassInfo = new PostClassInfo();
        BeanUtils.copyProperties(postsInfo,postClassInfo);
        PostClassInfo save = postClassInfoRepository.save(postClassInfo);
        return Result.ok().setData(save);
    }
}
